package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.frame.deal.base.BaseFrame;

import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;
import com.frame.work.mapper.SelectDeviceMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 86型人体感应面板
 */
@Data
public class BaLiuXingRenTiGanYingMianBan_0FAA8105 extends BaseFrame {

    private String alarmState;
    private String sosState;
    private String nightLightTime;
    private String switchState;


    public BaLiuXingRenTiGanYingMianBan_0FAA8105(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setAlarmState(getAlarmState(data));
        this.setSosState(getSosState(data));
        this.setNightLightTime(getNightLightTime(data));
        this.setSwitchState(getSwitchState(data));
        //推送及日志保存
        CoreLogicDeal();
        this.setActions(getActions());
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
    }

    /**
     * @Description:获取报警状态
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/1 12:56
     */
    public static String getAlarmState(String data) {

        return StringUtils.substring(data, data.length() - 8, data.length() - 6);
    }

    /**
     * @Description:获取求救信息
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/1 12:56
     */
    public static String getSosState(String data) {
        return StringUtils.substring(data, data.length() - 6, data.length() - 4);
    }

    /**
     * @Description:小夜灯设置时间
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/1 13:26
     */
    public static String getNightLightTime(String data) {
        return StringUtils.substring(data, data.length() - 4, data.length() - 2);
    }

    /**
     * @Description:获取开关状态信息
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/1 13:27
     */
    public static String getSwitchState(String data) {
        return StringUtils.substring(data, data.length() - 8, data.length() - 6);
    }

    /**
     * @Description:
     * @param:
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author:wjming
     * @Date:2021/2/1 13:31
     */
    @Override
    public Map<String, Object> getActions() {
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("workState", this.getDataType());
        actions.put("alarmState", this.alarmState);
        actions.put("sosState", this.sosState);
        actions.put("nightLightTime", this.nightLightTime);
        return actions;
    }

    private void CoreLogicDeal() {
        String type = "";
        String content = "";
        Boolean isSaveAndSend = false;

        SelectDeviceMapper testMapper = SpringUtil.getBean(SelectDeviceMapper.class);
        List<DeviceAssociatedInformation> list = testMapper.getDevice1(this.getDeviceMac());
        if (list.size() < 1) {
            return;
        }
        switch (this.getDataType()) {
            //正常
            case "00":
                break;
            //检测到求救状态发生变化
            case "40":
                type = StringUtils.equals("01", sosState) ? TypeEnum.WARNING.code() : TypeEnum.STATE.code();
                content = StringUtils.equals("01", sosState) ? "SOS报警" : "恢复";
                isSaveAndSend = true;
                break;
            //人体检测状态变化触发
            case "80":
                type = TypeEnum.STATE.code();
                content = StringUtils.equals("01", alarmState) ? "感应有人" : "恢复";
                isSaveAndSend = true;
                break;
            //人体/求救状态均发生变化触发
            case "C0":
                if (StringUtils.equals("01", this.alarmState) && StringUtils.equals("01", this.sosState)) {
                    content = "有人经过,且SOS报警";
                    type = TypeEnum.WARNING.code();
                }
                isSaveAndSend = true;
                break;
            default:
                break;
        }
        if (isSaveAndSend) {
            WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
        }
    }
}
